package com.ohs.ohsoa.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ohs.ohsoa.system.dto.MeetingDto;
import com.ohs.ohsoa.system.service.MeetingService;
import com.ohs.ohsoa.system.vo.MeetingVo;
import com.ohs.ohsoa.utils.auth.UserLoginToken;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 会议
 *
 * @author qhuang
 * @since 2021-11-10
 */
@Api(value = "会议controller", tags = "会议")
@RestController
@RequestMapping(value = "/meeting")
public class MeetingController {

    @Resource
    private MeetingService meetingService;

    /**
     * 获取会议列表信息
     *
     * @param pageParam 检索用参数
     * @return 会议列表信息
     */
    @UserLoginToken
    @PostMapping(value = "/select/list")
    @ApiOperation(httpMethod = "POST", value = "获取会议列表信息")
    public ResultBean<List<MeetingVo>> meetingSelectList(@RequestBody PageParam<MeetingDto> pageParam) {

        List<MeetingVo> res = meetingService.meetingSelectList(pageParam);
        return ResultBean.success(res);
    }

    /**
     * 获取会议详细信息
     *
     * @param meetingDto 会议数据
     * @return 会议详细信息
     */
    @UserLoginToken
    @PostMapping(value = "/select")
    @ApiOperation(httpMethod = "POST", value = "获取会议详细信息")
    public ResultBean<MeetingVo> meetingSelectByID(@RequestBody MeetingDto meetingDto) {

        MeetingVo meetingVo = meetingService.meetingSelectByID(meetingDto);
        return ResultBean.success(meetingVo);
    }

    /**
     * 新增会议信息
     *
     * @param meetingDto 会议数据
     * @return 新增结果
     */
    @UserLoginToken
    @PostMapping(value = "/insert")
    @ApiOperation(httpMethod = "POST", value = "新增会议信息")
    public ResultBean<Boolean> meetingInsert(@RequestBody @Valid MeetingDto meetingDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        meetingDto.setMInstime(nowTime);
        return ResultBean.success(meetingService.meetingInsert(meetingDto));
    }

    /**
     * 删除会议
     *
     * @param meetingDto 会议数据
     * @return 删除结果
     */
    @UserLoginToken
    @DeleteMapping(value = "/delete")
    @ApiOperation(httpMethod = "DELETE", value = "删除会议")
    public ResultBean<Boolean> meetingDelete(@RequestBody MeetingDto meetingDto) {
        return ResultBean.success(meetingService.meetingDelete(meetingDto));
    }

    /**
     * 修改会议信息
     *
     * @param meetingDto 会议数据
     * @return 修改结果
     */
    @UserLoginToken
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "修改会议信息")
    public ResultBean<Boolean> meetingUpdate(@RequestBody @Valid MeetingDto meetingDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        meetingDto.setMUpdtime(nowTime);
        return ResultBean.success(meetingService.meetingUpdate(meetingDto));
    }
}
